/*
 * mainapp.cpp
 *
 *  Created on: 2011-5-10
 *      Author: chenl
 */

#include "mainapp.h"
#include "config.h"
#include "processmgt.h"
#include "ps_error.h"
#include "common/common_type.h"
#include "common/common_delay.h"
#include "global.h"

#ifndef NOTEST
extern void AddTasks(unsigned n);
#endif


CMainApp::CMainApp() : logEngine(DEFAULT_PS_LOGPREFIX), logThread_(&CREATE_LOGQUEUE_INSTANCE())
{

}

CMainApp::~CMainApp()
{

}

int32_t CMainApp::Initialize()
{
	int32_t ret = S_OK;
	//初始化配置
	ret = CREATE_PARAMCONFIG_INSTANCE().Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing PARAMCONFIG failed! ret=0x%08x", ret);
		return ret;
	}

	//初始化定時器容器
	ret = CREATE_TIMERMGT_INSTANCE().Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing TIMERMGT failed! ret=0x%08x", ret);
		return ret;
	}

	//初始化日誌隊列
	ret = CREATE_LOGQUEUE_INSTANCE().Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing LOGQUEUE failed! ret=0x%08x", ret);
		return ret;
	}

	//初始化進程管理容器
	ret = CREATE_PROCESSMGT_INSTANCE().Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing PROCESSMGT failed! ret=0x%08x", ret);
		return ret;
	}

	//初始化日誌綫程
	ret = logThread_.Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing log thread failed! ret=0x%08x", ret);
		return ret;
	}
	//初始化調度綫程
	ret = psThread_.Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing ps thread failed! ret=0x%08x", ret);
		return ret;
	}
	//初始化UI綫程
	ret = uiThread_.Initialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Initializing ui thread failed! ret=0x%08x", ret);
		return ret;
	}

#ifndef NOTEST
	AddTasks(5);
#endif

	return ret;
}

int32_t CMainApp::Uninitialize()
{
	int32_t ret = S_OK;

	//銷毀UI綫程
	ret = uiThread_.Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing ui thread failed! ret=0x%08x", ret);
		return ret;
	}

	//銷毀調度綫程
	ret = psThread_.Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing ps thread failed! ret=0x%08x", ret);
		return ret;
	}

	//銷毀日誌綫程
	ret = logThread_.Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing log thread failed! ret=0x%08x", ret);
		return ret;
	}

	//銷毀進程管理容器
	ret = GET_PROCESSMGT_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing PROCESSMGT failed! ret=0x%08x", ret);
		return ret;
	}

	//銷毀定時器容器
	ret = CREATE_TIMERMGT_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing TIMERMGT failed! ret=0x%08x", ret);
		return ret;
	}

	//銷毀日誌隊列
	ret = CREATE_LOGQUEUE_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing LOGQUEUE failed! ret=0x%08x", ret);
		return ret;
	}

	//銷毀配置
	ret = GET_PARAMCONFIG_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Uninitializing PARAMCONFIG failed! ret=0x%08x", ret);
		return ret;
	}
	return ret;
}

void CMainApp::Start()
{
	//啓動日誌綫程
	logThread_.Start();
	//啓動圖形綫程
	uiThread_.Start();
	//啓動模擬器綫程
	psThread_.Start();
}

void CMainApp::Run()
{
	MainAppCmd cmd = enmMainAppCmd_Nop;
	while(1)
	{
		switch(cmd)
		{
		case enmMainAppCmd_Reload:
			Reload();
			break;
		case enmMainAppCmd_Term:
			Terminate();
			break;
		case enmMainAppCmd_Nop:
		default:
			;
		}
		DELAY(enmDefaultThreadExecutePeriod);
	}
}

int32_t CMainApp::Terminate()
{
	int32_t ret = S_OK;
	ret = uiThread_.Terminate();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "UI thread failed to terminate!");
	}
	ret = psThread_.Terminate();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "PS thread failed to terminate!");
	}
	ret = logThread_.Terminate();
	if (0 > ret)
	{
		logEngine.WriteLog(enmLogLevel_ERROR, "Log thread failed to terminate!");
	}
	return ret;
}

int32_t CMainApp::Resume()
{
	return S_OK;
}

int32_t CMainApp::Reload()
{
	return S_OK;
}

int32_t CMainApp::ProcessCommand(int argc, char **argv)
{
	return S_OK;
}
